<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar> </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.scOrder.code }}
        </a>
      </template>
      <template #summary>
        <a-table-summary fixed>
          <a-table-summary-row>
            <a-table-summary-cell :index="0" :col-span="3">合计报工数</a-table-summary-cell>
            <!-- <a-table-summary-cell :index="1"></a-table-summary-cell>
            <a-table-summary-cell :index="2"></a-table-summary-cell> -->
            <a-table-summary-cell :index="3" :col-span="9"></a-table-summary-cell>
            <a-table-summary-cell :index="10" :style="{ textAlign: 'right' }"> {{ totalIQty }}</a-table-summary-cell>
          </a-table-summary-row>
        </a-table-summary>
      </template>


      <!-- 在表格底部添加汇总行 -->
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsMoRepMesOrderTaskRepList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent,onMounted,ref,computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    mesOrderTaskRepDelete,
    mesOrderTaskRepListData,
  } from '/@/api/mes/mo/rep/mesOrderTaskRep';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';

  const { t } = useI18n('mo.rep.mesOrderTaskRep');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('任务报工管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('订单号'),
        field: 'scOrder.code',
        component: 'Input',
      },
      {
        label: t('机台'),
        field: 'basLine.name',
        component: 'Input',
      },
      {
        label: t('工序编码'),
        field: 'operCode',
        component: 'Input',
      },
      {
        label: t('工序名称'),
        field: 'basOper.name',
        component: 'Input',
      },
      {
        label: t('存货编码'),
        field: 'invCode',
        component: 'Input',
      },
      {
        label: t('报工人'),
        field: 'createByName',
        component: 'Input',
      },
      {
        label: t('操作工'),
        field: 'operator',
        component: 'Input',
      },
      {
        label: t('报工日期'),
        field: 'createDate',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          showTime: false,
        },
      },
    ],
    fieldMapToTime: [['createDate', ['createDate_gte', 'createDate_lte']]],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('订单号'),
      dataIndex: 'scOrder.code',
      key: 'sc.code',
      sorter: true,
      width: 120,
      align: 'left',
      // slot: 'firstColumn',
      fixed: 'left',
    },
    {
      title: t('行号'),
      dataIndex: 'scOrder.sortNum',
      key: 'sc.sort_num',
      sorter: true,
      width: 60,
      align: 'center',
      fixed: 'left',
    },
    {
      title: t('订单子表ID'),
      dataIndex: 'modid',
      key: 'a.modid',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('机台'),
      dataIndex: 'basLine.name',
      key: 'line.name',
      sorter: true,
      width: 120,
      align: 'left',
    },
    {
      title: t('工序编码'),
      dataIndex: 'operCode',
      key: 'a.oper_code',
      sorter: true,
      width: 120,
      align: 'left',
    },
    {
      title: t('工序名称'),
      dataIndex: 'basOper.name',
      key: 'oper.name',
      sorter: true,
      width: 120,
      align: 'left',
    },
    {
      title: t('存货编码'),
      dataIndex: 'invCode',
      key: 'a.inv_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('存货名称'),
      dataIndex: 'basInv.cinvname',
      key: 'inv.cinvname',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('规格型号'),
      dataIndex: 'basInv.cinvstd',
      key: 'inv.cinvstd',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('计件单价'),
      dataIndex: 'iprice',
      key: 'a.iprice',
      sorter: true,
      width: 100,
      align: 'right',
    },
    {
      title: t('报工人'),
      dataIndex: 'createByName',
      key: 'a.create_by_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('报工数'),
      dataIndex: 'iqty',
      key: 'a.iqty',
      sorter: true,
      width: 100,
      align: 'right',
    },
    {
      title: t('操作工'),
      dataIndex: 'operator',
      key: 'a.operator',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('报工日期'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 160,
      align: 'center',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑任务报工'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'mo:rep:mesOrderTaskRep:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除任务报工'),
        popConfirm: {
          title: t('是否确认删除任务报工'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'mo:rep:mesOrderTaskRep:edit',
      },
    ],
  };

  onMounted(() => {
    // 页面挂载后立即尝试加载数据以初始化总计
    console.log('onMounted==')
    reload();
  });
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm,getDataSource}] = useTable({

    api: mesOrderTaskRepListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    // actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });
  // 计算总报工数

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  // 计算总报工数
  const totalIQty = computed(() => {
    // 计算总报工数
    const tableData = getDataSource();
    // const tableData = getDataSource.value;
    return tableData.reduce((total, item) => total + (item.iqty || 0), 0);
  });

  async function handleDelete(record: Recordable) {
    const res = await mesOrderTaskRepDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
